package cate.game.server;

import cate.common.C2GameCommon;
import cate.game.GameBody;
import cp.server.CpServerTemplate;
import cp.solution.business.dog.cmd.StartGameCmd;
import cp.solution.rpc.RpcDebug;
import cp.solution.rpc.bridge.FaxRpcSender;
import easy.socket.EasySocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;
import reactor.core.publisher.Hooks;

// MongoAutoConfiguration.class, MongoDataAutoConfiguration.class

@Slf4j
@EnableGameServer
@SpringBootApplication(exclude = {MongoAutoConfiguration.class})
public class GameServerApplication  {

    public static StartGameCmd startCmd = new StartGameCmd();
    public static ApplicationContext ctx;

    public static String version() {
        return "2023-10-21 10:58";
    }

    public static long startTime = System.currentTimeMillis();

    public static void main(String[] args) {
        RpcDebug.enableDebugLog = false;
        FaxRpcSender.debug = false;
        startCmd.parseFromArgs(args);
        Hooks.onErrorDropped(e -> {});
        new SpringApplicationBuilder(GameServerApplication.class).run(args);
        CpServerTemplate.printVersion(log, version(), EasySocket.version(), C2GameCommon.version(), GameBody.version());
        log.info("游戏服启动完毕 本次启动耗时：{} ms", (System.currentTimeMillis()-startTime));
    }

    /**
     * 强制退出
     */
    public static void shutdownForce(String message) {
        log.info("执行强制退出！ 因为 {}", message);
        Runtime.getRuntime().halt(0);
    }

    /**
     * 退出
     */
    public static void shutdown(String message) {
        log.info("尝试执行shutdown, 因为 {}", message);
        if (ctx == null) {
            log.error("GameServerApplication.ctx==null shutdown拒绝执行");
            return;
        }
        int exitCode = SpringApplication.exit(ctx);
        System.exit(exitCode);
    }
}
